package com.flx.dis.read.write.split.config;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Author: Fenglixiong
 * @Date: 2021/6/16 18:19
 * @Description: 用来切换数据源，如果@Master或者增删改采用主库
 * 如果是@Slave或者查找采用从库
 */
@Aspect
@Component
public class DataSourceAop {

    @Pointcut("@annotation(com.flx.dis.read.write.split.annotion.Master) " +
            "|| execution(* com.flx.dis.read.write.split.service..*.insert*(..)) " +
            "|| execution(* com.flx.dis.read.write.split.service..*.add*(..)) " +
            "|| execution(* com.flx.dis.read.write.split.service..*.update*(..)) " +
            "|| execution(* com.flx.dis.read.write.split.service..*.edit*(..)) " +
            "|| execution(* com.flx.dis.read.write.split.service..*.delete*(..)) " +
            "|| execution(* com.flx.dis.read.write.split.service..*.remove*(..))")
    public void writePointcut() {

    }

    @Pointcut("!@annotation(com.flx.dis.read.write.split.annotion.Master) " +
            "&& (@annotation(com.flx.dis.read.write.split.annotion.Slave) || " +
            "execution(* com.flx.dis.read.write.split.service..*.select*(..)) ||" +
            "execution(* com.flx.dis.read.write.split.service..*.get*(..)))")
    public void readPointcut() {

    }

    @Before("writePointcut()")
    public void write() {
        DBContextHolder.master();
    }

    @Before("readPointcut()")
    public void read() {
        DBContextHolder.slave();
    }

}
